package sweep;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.Serial;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
import java.io.*;

public class Wait extends JFrame {

    private static DataOutputStream out;

    private Timer timer;

    public Wait(Socket client, String rival) {
        JLabel RivalLabel = new JLabel("Please wait the rival to start.");
        timer = new Timer(100, new TimerHandler(client, rival));
        timer.start();
        RivalLabel.setBounds(50, 130 , 200 , 25);
        add(RivalLabel);
        add(new JLabel(""));
    }

    // 处理 Server 消息
    class TimerHandler implements ActionListener {

        Socket client;
        String rival;

        public TimerHandler(Socket client, String rival) {this.client = client; this.rival = rival;}

        @Override
        public void actionPerformed(java.awt.event.ActionEvent e) {
            try {
                String msg = Client.getMessage();
                String[] message = msg.split(" ");
                // System.out.println(msg);
                // 游戏开始
                if (message[0].equals("game_start") == true) {
                    System.out.println("start");
                    Sweep.startGameBoard(client, rival);
                    dispose();
                }
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
    }
}